package etcdcli

import (
	"common/config"
	"common/logs"
	"errors"
	"fmt"
	"time"

	clientv3 "go.etcd.io/etcd/client/v3"
)

var V3Client *clientv3.Client

func GetInstance(etcdConf config.EtcdConf) (*clientv3.Client, error) {
	var err error
	if V3Client != nil {
		return V3Client, nil
	}
	fmt.Println(fmt.Sprintf("config.Conf.Etcd:%v", etcdConf))
	V3Client, err = clientv3.New(clientv3.Config{
		Endpoints:   etcdConf.Addrs,
		DialTimeout: time.Duration(etcdConf.DialTimeout) * time.Second,
		Username:    "root",
		Password:    "root",
	})
	if err != nil {

		logs.Fatal("etcdcli init err:%s", err.Error())
		fmt.Println(err)
		return V3Client, errors.New("etcdcli 链接错误")
	}
	return V3Client, nil
}
